using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YJ.Utility
{
    public class Config
    {
        /// <summary>
        /// 平台连接字符串
        /// </summary>
        public static string PlatformConnectionStringMSSQL
        {
            get
            {
                 return System.Configuration.ConfigurationManager.ConnectionStrings["PlatformConnection"].ConnectionString;
            }
        }
        /// <summary>
        /// 平台连接字符串
        /// </summary>
        public static string PlatformConnectionStringORACLE
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["PlatformConnectionOracle"].ConnectionString;
            }
        }

        /// <summary>
        /// 平台连接字符串
        /// </summary>
        public static string PlatformConnectionStringMySql
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["PlatformConnectionMySql"].ConnectionString;
            }
        }
    
        /// <summary>
        /// 数据库类型
        /// </summary>
        public static string DataBaseType
        {
            get
            {
                string dbType = System.Configuration.ConfigurationManager.AppSettings["DatabaseType"];
                return dbType.IsNullOrEmpty() ? "MSSQL" : dbType.ToUpper();
            }
        }
        /// <summary>
        /// 数据库类型
        /// </summary>
        public static string DataBaseType1
        {
            get
            {
                string dbType = System.Configuration.ConfigurationManager.AppSettings["DatabaseType"];
                return dbType.IsNullOrEmpty() ? "SqlServer" : (dbType.ToUpper().Equals("MSSQL")? "SqlServer": dbType.ToUpper());
            }
        }
        /// <summary>
        /// 系统初始密码
        /// </summary>
        public static string SystemInitPassword
        {
            get
            {
                string pass = System.Configuration.ConfigurationManager.AppSettings["InitPassword"];
                return pass.IsNullOrEmpty() ? "111" : pass.Trim();
            }
        }
        /// <summary>
        /// 文件及附件目录
        /// </summary>
        public static string FilePath
        {
            get
            {
                string filePath = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
                return filePath.IsNullOrEmpty() ? "d:\\YunJianFiles\\" : filePath.EndsWith("\\") ? filePath : filePath + "\\";
            }
        }
        /// <summary>
        /// 得到当前主题
        /// </summary>
        public static string Theme
        {
            get
            {
                var cookie = System.Web.HttpContext.Current.Request.Cookies["theme_platform"];
                return cookie != null && !cookie.Value.IsNullOrEmpty() ? cookie.Value : "Blue";
            }
        }

        /// <summary>
        /// 允许上传的文件类型
        /// </summary>
        public static string UploadFileType
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["UploadFileType"];
            }
        }

        /// <summary>
        /// 是否在线演示，演示不能修改密码删除用户等操作
        /// </summary>
        public static bool IsDemo
        {
            get
            {
                return "1".Equals(System.Configuration.ConfigurationManager.AppSettings["IsDemo"]);
            }
        }

        /// <summary>
        /// 日期格式
        /// </summary>
        public static string DateFormat
        {
            get
            {
                return "yyyy-MM-dd";
            }
        }

        /// <summary>
        /// 日期时间格式
        /// </summary>
        public static string DateTimeFormat
        {
            get
            {
                return "yyyy-MM-dd HH:mm";
            }
        }

        /// <summary>
        /// 日期时间格式(带秒)
        /// </summary>
        public static string DateTimeFormatS
        {
            get
            {
                return "yyyy-MM-dd HH:mm:ss";
            }
        }

        /// <summary>
        /// 系统表，用于在数据库管理时不能对系统表进行操作
        /// </summary>
        public static List<string> SystemDataTables
        {
            get
            {
                List<string> tableList = new List<string>();
                string tables = System.Configuration.ConfigurationManager.AppSettings["SystemTables"];
                if (tables.IsNullOrEmpty())
                {
                    return tableList;
                }
                foreach (string table in tables.Split(','))
                {
                    if (!table.IsNullOrEmpty())
                    {
                        tableList.Add(table);
                    }
                }
                return tableList;
            }
        }

        /// <summary>
        /// 系统外网地址
        /// </summary>
        public static string WebUrl
        {
            get
            {
                    return System.Configuration.ConfigurationManager.AppSettings["WebUrl"];
            }
        }
        /// <summary>
        /// 微信appid
        /// </summary>
        public static string wxqy_CorpID
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["appid"];
            }
        }
        /// <summary>
        /// 微信secret
        /// </summary>
        public static string wxqy_Secret
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["secret"];
            }
        }
        /// <summary>
        /// 系统所在目录
        /// </summary>
        public static string BaseUrl
        {
            get
            {
                string baseUrl = System.Web.HttpRuntime.AppDomainAppVirtualPath;
                return baseUrl == "/" ? "" : baseUrl;
            }
        }
        /// <summary>
        /// 得到用户令牌
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static string GetTokenByUserId(Guid userID)
        {
            Guid newid = Guid.NewGuid();
            return (userID.ToString() + "|" + newid.ToString()).DesEncrypt();
        }


        /// <summary>
        /// 从TOKEN里得到用户ID
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static Guid? GetUserIdByToken(string token)
        {
            if (token.IsNullOrEmpty())
            {
                return new Guid?();
            }
            string str = token.DesDecrypt();
            if (str.IsNullOrEmpty())
            {
                return new Guid?();
            }
            string[] strArray = str.Split('|');
            if (strArray == null || strArray.Length != 2)
            {
                return new Guid?();
            }
            string guid = strArray[0];
            return guid.IsGuid() ? guid.ToGuid() : new Guid?();
        }
    }
}
